{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [],
   "source": [
    ":dep nalgebra = \"0.33.0\" # 引入第三方依赖\n",
    ":dep approx = \"0.5.0\" # 引入第三方依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "no function or associated item named `from_rotation_zyx` found for struct `Matrix<_, Const<4>, Const<4>, ArrayStorage<_, 4, 4>>` in the current scope",
     "output_type": "error",
     "traceback": [
      "    let rotation_matrix = Matrix4::from_rotation_zyx(0.5, 0.5, 0.5);",
      "                                   ^^^^^^^^^^^^^^^^^ function or associated item not found in `Matrix<_, Const<4>, Const<4>, ArrayStorage<_, 4, 4>>`",
      "no function or associated item named `from_rotation_zyx` found for struct `Matrix<_, Const<4>, Const<4>, ArrayStorage<_, 4, 4>>` in the current scope"
     ]
    }
   ],
   "source": [
    "use nalgebra::{Vector3, Matrix4};\n",
    "\n",
    "fn main() {\n",
    "    // 点列表\n",
    "    let points = vec![(1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0)];\n",
    "\n",
    "    // 将点列表转换为Vector3<f64>类型的向量列表\n",
    "    let mut vectors = points.iter().map(|(x, y, z)| Vector3::new(x, y, z)).collect::<Vec<_>>();\n",
    "\n",
    "    // 创建旋转矩阵和平移向量\n",
    "    let rotation_matrix = Matrix4::from_rotation_zyx(0.5, 0.5, 0.5);\n",
    "    let translation_vector = Vector3::new(1.0, 2.0, 3.0);\n",
    "\n",
    "    // 旋转和平移向量列表\n",
    "    let mut transformed_vectors = vectors.iter().map(|v| rotation_matrix * v + translation_vector).collect::<Vec<_>>();\n",
    "\n",
    "    // 将旋转和平移后的向量列表转换回点列表\n",
    "    let transformed_points = transformed_vectors.iter().map(|v| (v.x, v.y, v.z)).collect::<Vec<_>>();\n",
    "\n",
    "    println!(\"原始点列表: {:?}\", points);\n",
    "    println!(\"旋转和平移后的点列表: {:?}\", transformed_points);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "nalgebra 如何将5个列向量(x,y,z)构成的点，组合成一个 3乘以5的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [],
   "source": [
    "use nalgebra::{Matrix, Vector3};\n",
    "\n",
    "fn main() {\n",
    "    // 点列表\n",
    "    let points = vec![(1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0), (10.0, 11.0, 12.0), (13.0, 14.0, 15.0)];\n",
    "\n",
    "    // 将点列表转换为Vector3<f64>类型的向量列表\n",
    "    let vectors = points.iter().map(|(x, y, z)| Vector3::new(x, y, z)).collect::<Vec<_>>();\n",
    "\n",
    "    // 将向量列表转换为Matrix<f64>类型的矩阵\n",
    "    let matrix = Matrix::from_vec(vectors);\n",
    "\n",
    "    println!(\"矩阵:\\n{}\", matrix);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "rust"
    }
   },
   "outputs": [],
   "source": [
    "use nalgebra::{Matrix, Vector3};\n",
    "\n",
    "fn main() {\n",
    "    // 点列表\n",
    "    let points = vec![(1.0, 2.0, 3.0), (4.0, 5.0, 6.0), (7.0, 8.0, 9.0), (10.0, 11.0, 12.0), (13.0, 14.0, 15.0)];\n",
    "\n",
    "    // 将点列表转换为Vector3<f64>类型的向量列表\n",
    "    let vectors = points.iter().map(|(x, y, z)| Vector3::new(x, y, z)).collect::<Vec<_>>();\n",
    "\n",
    "    // 将向量列表转换为Matrix<f64>类型的矩阵\n",
    "    let matrix = Matrix::from_vec(vectors);\n",
    "\n",
    "    println!(\"矩阵:\\n{}\", matrix);\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在3D图形和计算机视觉中，旋转次序通常根据旋转轴的顺序来确定。在三维空间中，旋转的次序对结果有影响，因为旋转是三维空间中的线性变换。\n",
    "\n",
    "假设我们有一个旋转矩阵 R，它表示绕x轴、y轴和z轴的旋转。如果我们按照x-y-z的顺序进行旋转，那么我们可以表示这个旋转矩阵为：\n",
    "\n",
    "R = Rz * Ry * Rx\n",
    "\n",
    "如果我们按照y-x-z的顺序进行旋转，那么我们可以表示这个旋转矩阵为：\n",
    "\n",
    "R = Rx * Rz * Ry\n",
    "\n",
    "因此，旋转的次序是很重要的，因为它可以改变旋转的结果。\n",
    "\n",
    "在Rust中，如果你使用的是nalgebra库，那么你可以使用 `Rotation` 结构体来表示旋转。这个结构体有一个 `axis` 字段，表示旋转的轴，和一个 `angle` 字段，表示旋转的角度。你可以使用 `Rotation::new` 函数来创建一个新的旋转。\n",
    "\n",
    "例如：\n",
    "\n",
    "```rust\n",
    "use nalgebra::{Rotation, Vector3};\n",
    "\n",
    "fn main() {\n",
    "    let rotation_x = Rotation::new(Vector3::x_axis(), std::f64::consts::PI / 2.0);\n",
    "    let rotation_y = Rotation::new(Vector3::y_axis(), std::f64::consts::PI / 2.0);\n",
    "    let rotation_z = Rotation::new(Vector3::z_axis(), std::f64::consts::PI / 2.0);\n",
    "\n",
    "    let rotation_matrix_x = rotation_x.to_matrix();\n",
    "    let rotation_matrix_y = rotation_y.to_matrix();\n",
    "    let rotation_matrix_z = rotation_z.to_matrix();\n",
    "\n",
    "    println!(\"Rotation matrix x:\");\n",
    "    println!(\"{:?}\", rotation_matrix_x);\n",
    "\n",
    "    println!(\"Rotation matrix y:\");\n",
    "    println!(\"{:?}\", rotation_matrix_y);\n",
    "\n",
    "    println!(\"Rotation matrix z:\");\n",
    "    println!(\"{:?}\", rotation_matrix_z);\n",
    "}\n",
    "```\n",
    "\n",
    "这将输出：\n",
    "\n",
    "```\n",
    "Rotation matrix x:\n",
    "Matrix3([\n",
    "    1.0,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    0.5773502691896258,\n",
    "    -0.8164965809277259,\n",
    "    0.0,\n",
    "    0.8164965809277259,\n",
    "    0.5773502691896258,\n",
    "])\n",
    "Rotation matrix y:\n",
    "Matrix3([\n",
    "    0.5773502691896258,\n",
    "    0.8164965809277259,\n",
    "    0.0,\n",
    "    -0.8164965809277259,\n",
    "    0.5773502691896258,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    1.0,\n",
    "])\n",
    "Rotation matrix z:\n",
    "Matrix3([\n",
    "    0.5773502691896258,\n",
    "    -0.8164965809277259,\n",
    "    0.0,\n",
    "    0.8164965809277259,\n",
    "    0.5773502691896258,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    0.0,\n",
    "    1.0,\n",
    "])\n",
    "```\n",
    "\n",
    "从这些结果可以看出，旋转的次序对旋转矩阵有影响。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Rust",
   "language": "rust",
   "name": "rust"
  },
  "language_info": {
   "codemirror_mode": "rust",
   "file_extension": ".rs",
   "mimetype": "text/rust",
   "name": "Rust",
   "pygment_lexer": "rust",
   "version": ""
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
